import { PayloadAction, createSlice } from "@reduxjs/toolkit"

interface UserState {
  username: string
  nickname: string
  avator: string
  id: string
  isLogin: boolean
}


const initialState: UserState = {
  username: '',
  nickname: '',
  avator: '',
  id: '',
  isLogin: false
}

const userSlice = createSlice({
  name: 'userSlice',
  initialState,
  reducers: {
    loginSuccess(state, action: PayloadAction<UserState>) {
        state.avator = action.payload.avator
        state.nickname = action.payload.nickname
        state.id = action.payload.id
        state.username = action.payload.username
        state.isLogin = true
    },
    loginFail(state) {
      state.avator = ''
      state.nickname = ''
      state.id = ''
      state.username = ''
      state.isLogin = false
    },
    setAvator(state, action: PayloadAction<string>) {
      state.avator = action.payload
    }
  }
})

export default userSlice.reducer

export const { loginFail, loginSuccess, setAvator } = userSlice.actions